<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/14
 * Time: 15:03
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class WorkmanAssignmentModel
{
    const TABLE = 'workman_assignment';

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function costLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where($data)
            ->select(DB::raw('sum(totalPrice) as totalPrice'))
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($data){
                if (!empty($data['projectId'])){
                    $query->where('projectId',$data['projectId']);
                }
                if (!empty($data['areaId'])){
                    $query->where('areaId',$data['areaId']);
                }
                if (!empty($data['sectionId'])){
                    $query->where('sectionId',$data['sectionId']);
                }
                if (!empty($data['groupId'])){
                    $query->where('groupId',$data['groupId']);
                }
                if (!empty($data['professionId'])){
                    $query->where('professionId',$data['professionId']);
                }
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('completeTime',[$data['startTime'],$data['endTime']]);
                }
                if (isset($data['status'])){
                    if (!is_array($data['status'])){
                        $query->where('status',$data['status']);
                    }else{
                        $query->whereIn('status',$data['status']);
                    }
                }
            })->sum('totalPrice');
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getGroups(array $data){
        return DB::table(self::TABLE)
            ->leftJoin('group as g','g.id','=',self::TABLE.'.groupId')
            ->where(function ($query) use ($data){
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('completeTime',[$data['startTime'],$data['endTime']]);
                }
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['areaId'])){
                    $query->where('areaId',$data['areaId']);
                }
                if (!empty($data['sectionId'])){
                    $query->where('sectionId',$data['sectionId']);
                }
                if (!empty($data['groupId'])){
                    $query->where('groupId',$data['groupId']);
                }
            })
            ->groupBy('groupId')
            ->select('groupId','g.name as groupName')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('assignment as a','a.id','=',self::TABLE.'.assignmentId')
            ->leftJoin('assignment_label as al','al.id','=','a.labelId')
            ->leftJoin('unit as u','u.id','=','a.unitId')
            ->leftJoin('admin','admin.id','=',self::TABLE.'.adminId')
            ->where(function($query) use ($data){
                $query->where(self::TABLE.'.projectId',$data['projectId']);
                $query->where(self::TABLE.'.sectionId',$data['sectionId']);
                $query->where(self::TABLE.'.groupId',$data['groupId']);
                if (isset($data['batch']) && $data['batch'] != ''){
                    $query->where(self::TABLE.'.batch',$data['batch']);
                }
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('completeTime',[$data['startTime'],$data['endTime']]);
                }
            })
            ->select(self::TABLE.'.*','a.name as assignmentName','a.labelId','al.name as labelName','u.name as unitName','admin.name as adminName')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return array
     */
    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $data
     * @return int
     */
    public function getBatchList(array $data)
    {
        $result = DB::table(self::TABLE)
            ->where(function($query)use($data){
                $query->where(self::TABLE.'.projectId',$data['projectId']);
                $query->where(self::TABLE.'.sectionId',$data['sectionId']);
                $query->where(self::TABLE.'.groupId',$data['groupId']);
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('completeTime',[$data['startTime'],$data['endTime']]);
                }
            })->distinct()->orderBy('batch')
            ->pluck('batch');
        return $result;
    }

    public function getBatch(array $data)
    {
        $result = DB::table(self::TABLE)
            ->where(function($query)use($data){
                $query->where(self::TABLE.'.projectId',$data['projectId']);
                $query->where(self::TABLE.'.sectionId',$data['sectionId']);
                $query->where(self::TABLE.'.groupId',$data['groupId']);
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('completeTime',[$data['startTime'],$data['endTime']]);
                }
            })
            ->max('batch');
                if (!is_int($result)){
                    $result = 0;
                }
        return $result;
    }

    public function update(array $data, array $where)
    {
        return DB::table(self::TABLE)->where($where)->update($data);

    }

    public function updateStatus(array $where,array $data){
        return DB::table(self::TABLE)
            ->where(function($query)use($where){
                if (!empty($where['id'])){
                    $query->where('id',$where['id']);
                }
                if (!empty($where['employeeId'])){
                    $query->where('employeeId',$where['employeeId']);
                }
                if (!empty($where['professionId'])){
                    $query->where('professionId',$where['professionId']);
                }
                if (!empty($where['sectionId'])){
                    $query->where('sectionId',$where['sectionId']);
                }
                if (!empty($where['areaId'])){
                    $query->where('areaId',$where['areaId']);
                }
                if (!empty($where['projectId'])){
                    $query->where('projectId',$where['projectId']);
                }
                if (!empty($where['startTime']) && !empty($where['endTime'])){
                    $query->whereBetween('completeTime',[$where['startTime'],$where['endTime']]);
                }
                if (!empty($where['status'])){
                    $query->where('status',$where['status']);
                }
                if (!empty($where['adminId'])){
                    $query->where('adminId',$where['adminId']);
                }
            })
            ->update($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }
}